import { Component, OnInit } from '@angular/core';
import { UserApiService } from 'src/app/services/user/user-api.service';

@Component({
  selector: 'app-user-list',
  templateUrl: './user-list.component.html',
  styleUrls: ['./user-list.component.less']
})
export class UserListComponent implements OnInit {

  pageIndex = 0;
  pageSize = 10;
  total: Number = 1;
  dataSet = [];
  loading = true;
  sortValue = null;
  sortKey = null;

  searchGenderList: string[] = [];

  sort(sort: { key: string; value: string }): void {
    this.sortKey = sort.key;
    this.sortValue = sort.value;
    this.searchData();
  }

  constructor(private userApi: UserApiService) { }

  searchData(reset: boolean = false): void {
    if (reset) {
      this.pageIndex = 0;
    }
    this.loading = true;
    this.userApi.users(this.pageSize, this.pageIndex).subscribe(
      (response) => {
        this.loading = false;
        this.total = response.data.totalElements;
        this.dataSet = response.data.content;
      }
    );
  }

  updateFilter(value: string[]): void {
    this.searchGenderList = value;
    this.searchData(true);
  }

  /**
   * 更新用户状态
   * @param {number} id
   */
  userStatusClick(id: number): void {
    this.userApi.userStatus(id).subscribe((response) => {
      if (response.success) {
        this.searchData(true)
      }
    })
  }

  ngOnInit(): void {
    this.searchData();
  }
}
